<?php
namespace app\api\validate\v2;

use app\api\validate\Base;

/**
 * 行为验证器
 * @package app\menus\validate
 */
class Contract extends Base
{

    //定义验证规则
    protected $rule = [
        'user_id|用户'  => 'require|number',
        'user_name|用户姓名'  => 'require|chsDash',
        'user_card|用户身份证号'  => 'require|checkIdCard',
        'user_phone|用户手机号'  => 'require|regex:/^1[3456789]\d{9}$/',
        'water_num|水表数'  => 'require|float',
        'electricity_num|电表数'  => 'require|float',
        'type|合同类型'  => 'require|number',
        'house_id|房源ID'  => 'require|number',
        'ya_type|押金类型'  => 'require|number',
        'money_zu|租金'  => 'require|float',
        'money_ya|押金'  => 'require|float',
        'money_electricity|电费'  => 'require|float',
        'money_water|水费'  => 'require|float',
        'start_time|开始时间'  => 'require|date',
        'end_time|结束时间'  => 'require|date',
        'photo|图片'  => 'require|simpleVarchar',
        'attachment|附件'  => 'require|simpleVarchar',
        'contract_id|合同id'  => 'require|number',
        'house_id|房源id'  => 'require|number',
        'room_id|房间id'  => 'require|number',
        'water_num|用水吨数'  => 'require|float',
        'electricity_num|用电度数'  => 'require|float',
        'other_data|其他费用'  => 'require',
        'water_photo|水费图片'  => 'require|simpleVarchar',
        'electricity_photo|电费图片'  => 'require|simpleVarchar',
        'pay_log_id|缴费记录ID'  => 'require|number',
        'error_type|报修项目'  => 'require|number',
        'error_title|报修描述'  => 'require|simpleVarchar',
        'error_photo|报修图片'  => 'require|simpleVarchar',
        'biao_data|抄表数据'  => 'require',
        'msg_type|催缴类型'  => 'require|in:1,2',
        'user_type|租客类型'  => 'require|in:1,2',
        'rent_way|交租方式'  => 'require|number',
        'rent_day|交租日'  => 'require|between:1,30',
    ];

    //定义验证提示
    protected $message = [
        'area_id.require' => '请选择地区',
        'area_id.number' => '地区格式错误',
        'wx_num.require' => '微信号不能为空',
        'wx_num.chsDash' => '微信号错误',
    ];


    //定义验证场景
    protected $scene = [
        //更新
        'add'  =>  ['type','room_id','start_time','end_time','money_zu','money_ya','pay_time','user_type','user_name','user_card','user_phone','rent_way','rent_day'],
        'add_attachment'  =>  ['lng'],
        'add_photo'  =>  ['lat'],
        'addBiao'  =>  ['house_id','electricity_num','water_num','other_data'],
        'addBiaoList'  =>  ['biao_data'],
        'editBiao'  =>  ['pay_log_id','electricity_num','water_num','other_fee'],
        'addBiao_water_photo'  =>  ['water_photo'],
        'addBiao_electricity_photo'  =>  ['electricity_photo'],
        'biaoInfo'  =>  ['house_id'],
        'addOrder'  =>  ['pay_log_id'],
        'sendBiaoMessage'  =>  ['pay_log_id'],
        'sendHouseError'  =>  ['error_type','error_title'],
        'sendHouseError_error_photo'  =>  ['error_photo'],
        'offlinePayment'  =>  ['pay_log_id'],
        'sendMessage'  =>  ['type'],
    ];

    //过滤特殊字符
    public function IsVarchar($value,$rule='',$date='',$field=''){
        $file_array=[
            ['key'=>'desc','name'=>'简介'],
            ['key'=>'tags','name'=>'标签'],
            ['key'=>'address','name'=>'地址'],
            ['key'=>'error_photo','name'=>'图片'],
        ];
        $file_array=array_column($file_array,null,'key');
        $str='/SELECT|INSERT|UPDATE|DOCUMENT|EVAL|DELETE|SCRIPT|ALERT|UNION|INTO|LOAD_FILE|OUTFILE|select|insert|update|CR|document|LF|eval|delete|script|alert|\'|\/\*|\#|\--|\ --|\/|\*|\+|\=|\~|\*@|\*!|\$|\%|\^|\&|\(|\)|\/|\/\/|\.\.\/|\.\/|union|into|load_file|outfile/';
        if(!preg_match($str,$value)){
            return true;
        }else{
            return $file_array[$field]['name'].'格式错误';
        }
    }
    //过滤特殊字符
    public function simpleVarchar($value,$rule='',$date='',$field=''){
        $file_array=[
            ['key'=>'water_photo','name'=>'水表图片'],
            ['key'=>'electricity_photo','name'=>'电表图片'],
            ['key'=>'error_photo','name'=>'图片'],
        ];
        $file_array=array_column($file_array,null,'key');
        $str='/SELECT|INSERT|UPDATE|DOCUMENT|EVAL|DELETE|SCRIPT|ALERT|UNION|INTO|LOAD_FILE|OUTFILE|select|insert|update|CR|document|LF|eval|delete|script|alert|\'|\/\*|\#|\--|\ --|\*|\+|\=|\~|\*@|\*!|\$|\%|\^|\&|\(|\)|\.\.\/|\.\/|union|into|load_file|outfile/';
        if(!preg_match($str,$value)){
            return true;
        }else{
            return $file_array[$field]['name'].'格式错误';
        }
    }
    public function checkIdCard($idcard) {
        if(empty($idcard)){
            return false;
        }else{
            $idcard = strtoupper($idcard); # 如果是小写x,转化为大写X
            if(strlen($idcard) != 18 && strlen($idcard) != 15){
                return false;
            }
            # 如果是15位身份证，则转化为18位
            if(strlen($idcard) == 15){
                # 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
                if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) {
                    $idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9);
                } else {
                    $idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9);
                }
                # 加权因子
                $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                # 校验码对应值
                $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $checksum = 0;
                for ($i = 0; $i < strlen($idcard); $i++) {
                    $checksum += substr($idcard, $i, 1) * $factor[$i];
                }
                $idcard = $idcard . $code[$checksum % 11];
            }
            # 验证身份证开始
            $IDCardBody = substr($idcard, 0, 17); # 身份证主体
            $IDCardCode = strtoupper(substr($idcard, 17, 1)); # 身份证最后一位的验证码

            # 加权因子
            $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            # 校验码对应值
            $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $checksum = 0;
            for ($i = 0; $i < strlen($IDCardBody); $i++) {
                $checksum += substr($IDCardBody, $i, 1) * $factor[$i];
            }
            $validateIdcard = $code[$checksum % 11];    # 判断身份证是否合理
            if($validateIdcard != $IDCardCode){
                return false;
            }else{
                return true;
            }
        }
    }
}
